﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using ZX.BLL.Extend;
using ZX.SYS.Library;
using ZX.EHR.Library;
using ZX.Common;
using ZX.Common.Extend;
using ZX.DBModule;

namespace ZX.BLL
{
    public class BhrCandidateSelection : BaseBussnies
    {
        public override bool Insert(object entitys, out string msg)
        {
            bool rs = base.Insert(entitys, out msg);
            if (rs)
            {
                hrCandidateSelection obj = entitys as hrCandidateSelection;
                RunJob(obj,0);
            }
            return rs;
        }

        public override bool Update(object entitys, out string msg)
        {
            bool rs = base.Update(entitys, out msg);
            if (rs)
            {
                hrCandidateSelection obj = entitys as hrCandidateSelection;
                RunJob(obj,1);
            }
            return rs;
        }

        public override bool delete(object entitys, out string msg)
        {
            bool rs= base.delete(entitys, out msg);
            if (rs)
            {
                hrCandidateSelection obj = entitys as hrCandidateSelection;
                RunJob(obj,2);
            }
            return rs;
        }
        /// <summary>
        /// 业务逻辑处理
        /// </summary>
        /// <param name="obj">实体</param>
        /// <param name="function">逻辑功能0－新增，1－修改，2－删除</param>
        private void RunJob(hrCandidateSelection obj,byte function)
        {
            //招聘选拔时更改选拔状态       
            try
            {
                string condition = string.Empty;
                ZX.BLL.Extend.BaseBussnies bll = null;
                BaseEntity Instance = null;
                Instance = ZX.Common.Globals.getEntity("hrCandidate") as BaseEntity;
                bll = new ZX.BLL.Extend.BaseBussnies(Instance);
                condition = string.Format("ID='{0}'", obj.CandidateID);
                hrCandidate hrcandidate = bll.GetEntityByCondition(condition) as hrCandidate;
                if (function == 0 || function == 1) //新增或修改
                {
                    if (obj.StepResult == 2) //选拔不通过
                    {
                        if (obj.StepType == 1) //招聘初选
                        {
                            hrcandidate.PrimariesStatus = obj.StepResult;
                        }
                        else if (obj.StepType == 2) //招聘筛选
                        {
                            hrcandidate.FilterStatus = obj.StepResult;
                        }
                    }
                    else
                    {
                        if (obj.StepOverFlag == 1)//招聘选拔结束
                        {
                            if (obj.StepType == 1) //招聘初选
                            {
                                hrcandidate.PrimariesStatus = obj.StepResult;
                            }
                            else if (obj.StepType == 2) //招聘筛选
                            {
                                hrcandidate.FilterStatus = obj.StepResult;
                            }
                        }
                        else
                        {
                            ////先判断其他选拔记录中是否有结束的记录，如果没有则将人才档案选拔状态改为0，否则不变
                            //hrCandidateSelection hrSelectionInstance = ZX.Common.Globals.getEntity("hrCandidateSelection") as hrCandidateSelection;
                            //ZX.BLL.Extend.BaseBussnies hrSelectionBll = new ZX.BLL.Extend.BaseBussnies(hrSelectionInstance);                   
                            //if (obj.StepType == 1) //招聘初选
                            //{
                            //    condition = "StepOverFlag=1 and StepType=1";
                            //    IList<BaseEntity> list = hrSelectionBll.GetEntitysByCondition(condition);
                            //    if (list == null || list.Count == 0)
                            //    {
                            //        hrcandidate.PrimariesStatus = 0;
                            //    }
                            //}
                            //else if (obj.StepType == 2) //招聘筛选
                            //{
                            //    condition = "StepOverFlag=1 and StepType=2";
                            //    IList<BaseEntity> list = hrSelectionBll.GetEntitysByCondition(condition);
                            //    if (list == null || list.Count == 0)
                            //    {
                            //        hrcandidate.FilterStatus = 0;
                            //    }
                            //}
                            if (obj.StepType == 1) //招聘初选
                            {
                                hrcandidate.PrimariesStatus = 0;
                            }
                            else if (obj.StepType == 2) //招聘筛选
                            {
                                hrcandidate.FilterStatus = 0;
                            }
                        }
                    }
                    try
                    {
                        //将该应聘人其他选拔记录的结束状态为都修改为obj的结束状态，保证结束状态的同步
                        condition = string.Format("StepOverFlag!={0} and StepType={1}", obj.StepOverFlag, obj.StepType);
                        hrCandidateSelection hrSelectionInstance = ZX.Common.Globals.getEntity("hrCandidateSelection") as hrCandidateSelection;
                        ZX.BLL.Extend.BaseBussnies hrSelectionBll = new ZX.BLL.Extend.BaseBussnies(hrSelectionInstance);
                        IList<BaseEntity> list = hrSelectionBll.GetEntitysByCondition(condition);
                        if (list != null && list.Count > 0)
                        {
                            for (int i = 0; i < list.Count; i++)
                            {
                                hrCandidateSelection hrCS = list[i] as hrCandidateSelection;
                                hrCS.StepOverFlag = obj.StepOverFlag;
                                hrSelectionBll.Update(hrCS);
                            }
                        }
                    }
                    catch
                    { }
                }
                else //删除
                {
                    if (obj.StepType == 1) //招聘初选
                    {
                        hrcandidate.PrimariesStatus = 0;
                        hrcandidate.FilterStatus = 0;
                    }
                    else if (obj.StepType == 2) //招聘筛选
                    {
                        hrcandidate.FilterStatus = 0;
                    }
                }                              
                bll.Update(hrcandidate);
            }
            catch (Exception e)
            { }
        }
    }
}
